package com.fbs.wowo.finance.mapper;

import com.fbs.wowo.finance.entity.Bill;
import com.fbs.wowo.finance.vo.BillQuery;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface BillMapper {
    @Delete({
        "delete from bill",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @InsertProvider(type=BillSqlProvider.class, method="insertSelective")
    int insertSelective(Bill record);

    @Select({
        "select",
        "id, user_id, trade_id, trade_time, total_fee, is_income, other_side, refund, ",
        "trade_type, product_id, product_category, product_type, status, balance, scene, ",
        "tip",
        "from bill",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.VARCHAR),
        @Result(column="trade_id", property="tradeId", jdbcType=JdbcType.VARCHAR),
        @Result(column="trade_time", property="tradeTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="total_fee", property="totalFee", jdbcType=JdbcType.DECIMAL),
        @Result(column="is_income", property="isIncome", jdbcType=JdbcType.TINYINT),
        @Result(column="other_side", property="otherSide", jdbcType=JdbcType.VARCHAR),
        @Result(column="refund", property="refund", jdbcType=JdbcType.VARCHAR),
        @Result(column="trade_type", property="tradeType", jdbcType=JdbcType.TINYINT),
        @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
        @Result(column="product_category", property="productCategory", jdbcType=JdbcType.TINYINT),
        @Result(column="product_type", property="productType", jdbcType=JdbcType.TINYINT),
        @Result(column="status", property="status", jdbcType=JdbcType.TINYINT),
        @Result(column="balance", property="balance", jdbcType=JdbcType.DECIMAL),
        @Result(column="scene", property="scene", jdbcType=JdbcType.VARCHAR),
        @Result(column="tip", property="tip", jdbcType=JdbcType.VARCHAR)
    })
    Bill selectByPrimaryKey(String id);

    @UpdateProvider(type=BillSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Bill record);

    /**
     * 复合条件选择查询
     * @param
     * @return
     */
    @SelectProvider(type=BillSqlProvider.class, method="findSelective")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.VARCHAR),
            @Result(column="trade_id", property="tradeId", jdbcType=JdbcType.VARCHAR),
            @Result(column="trade_time", property="tradeTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="total_fee", property="totalFee", jdbcType=JdbcType.DECIMAL),
            @Result(column="is_income", property="isIncome", jdbcType=JdbcType.TINYINT),
            @Result(column="other_side", property="otherSide", jdbcType=JdbcType.VARCHAR),
            @Result(column="refund", property="refund", jdbcType=JdbcType.VARCHAR),
            @Result(column="trade_type", property="tradeType", jdbcType=JdbcType.TINYINT),
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="product_category", property="productCategory", jdbcType=JdbcType.TINYINT),
            @Result(column="product_type", property="productType", jdbcType=JdbcType.TINYINT),
            @Result(column="status", property="status", jdbcType=JdbcType.TINYINT),
            @Result(column="balance", property="balance", jdbcType=JdbcType.DECIMAL),
            @Result(column="scene", property="scene", jdbcType=JdbcType.VARCHAR),
            @Result(column="tip", property="tip", jdbcType=JdbcType.VARCHAR)
    })
    List<Bill> findSelective(BillQuery billQuery);

    /**
     * 复合条件查询 金额总数统计
     */
    @SelectProvider(type=BillSqlProvider.class, method="countSelective")
    BigDecimal countSelective(BillQuery billQuery);

    /**
     * 更新未发放的到已发放的
     * 某个用户id
     * 所有未发放的
     * 时间在上个月的
     * 收入
     */
    @Update({
            "update bill",
            "set status = #{statusUpdate,jdbcType=TINYINT}",
            "where user_id = #{userId,jdbcType=VARCHAR}",
            "and trade_time < #{deadline,jdbcType=TIMESTAMP}",
            "and status = #{statusQuery,jdbcType=TINYINT}"
    })
    void updateWallet(@Param("userId") String userId, @Param("deadline") Date deadline, @Param("statusQuery") Byte statusQuery, @Param("statusUpdate") Byte statusUpdate);

}